package com.wude.tools;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Helen on 2017/5/4.
 * <p>
 * 保存bitmap到手机内存
 */

public class SaveBitmapToFile {
    static String path = Environment.getExternalStorageDirectory() + "/快乐扫码/";// 将要保存图片的路径

    /**
     * 保存文件
     *
     * @param bitmaps
     * @throws IOException
     */
    public static List<File> saveFile(List<Bitmap> bitmaps) {
        List<File> files = new ArrayList<>();
        File dirFile = new File(path);
        if (!dirFile.exists()) {
            dirFile.mkdir();
        }
        for (int i = 0; i < bitmaps.size(); i++) {
            File myCaptureFile = new File(path + getCurrentTime() + ".png");
            BufferedOutputStream bos;
            try {
                bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
                bitmaps.get(i).compress(Bitmap.CompressFormat.PNG, 100, bos);
                bos.flush();
                bos.close();
                files.add(myCaptureFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
                Log.d("------", e.toString());
            }

        }
        return files;
    }

    /**
     * 得到当前系统的时间
     **/
    private static String getCurrentTime() {
//        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
//        String str = format.format(new Date());
        return System.currentTimeMillis() + "";
    }
}
